<?php

function create_handler($conn, $sql_show, $sql_create, $name)
{
	$result = $conn->query($sql_show);
	if($result->num_rows <= 0 || !in_array(array(0=>$name), $result->fetch_all(), true))
	{
		if($conn->query($sql_create))
		{
			return 1;
		}
		else
		{
			echo $conn->error;
			return 0;
		}
	}
	return 2;
}

function change_database($conn, $database_name)
{
	$result = $conn->query("use $database_name");
	if(!$result)
	{
		die("use $database_name error". $conn->error ."\n");
#reply2client(0, '10004');
	}
}


function query_handler($db, $sql)
{
	$result = $db->query($sql);
	if($result->num_rows <= 0)
	{
		return array();
	}
	return $result->fetch_all();
}

function query2array($array,$str_index, $keys)
{
	$my_array = array();
	$pre_index = 0;
	$end_index = count($array) - 1;
	if(!empty($str_index))
	{
		//echo "empty <br>";
		$index = explode(',', $str_index);
		sort($index);
		if(intval($index[0]) >= 0 && intval($index[1]) < $end_index)
		{
			$pre_index = intval($index[0]);
			$end_index = intval($index[1]);
		}
		elseif(intval($index[0]) >= 0 && intval($index[1]) >= $end_index)
		{
			$pre_index = intval($index[0]);
		}
	}
	//echo "$pre_index, $end_index <br>";
	$new_index = 0;
	for($i = $pre_index; $i <= $end_index; ++$i)
	{
		foreach($keys as $key_2 => $value_2)
		{
			//echo "$i $value_2 $key_2 $array[$i][$key_2]<br>";
			$my_array[$new_index][$value_2] = urlencode($array[$i][$key_2]);
		}
		$new_index++;
	}
/*
	foreach($array as $key => $value)
	{
		foreach($keys as $key_2 => $value_2)
		{
			//echo "$key $value_2 $key_2 $value[$key_2]<br>";
			$my_array[$key][$value_2] = $value[$key_2];
		}
	}*/
	return $my_array;
}

?>
